package com.zr.kucunkufangbiaoshi.controller.controller;


import com.zr.kucunkufangbiaoshi.controller.meiju.StatusEnum;
import com.zr.kucunkufangbiaoshi.controller.meiju.XiaLaVo;
import com.zr.kucunkufangbiaoshi.controller.pojo.DepotAddVo;
import com.zr.kucunkufangbiaoshi.controller.pojo.DepotSelectVo;
import com.zr.kucunkufangbiaoshi.controller.pojo.DepotUpdateStatus;
import com.zr.kucunkufangbiaoshi.controller.pojo.DepotUpdateVo;
import com.zr.kucunkufangbiaoshi.controller.service.DepotService;
import com.zr.kucunkufangbiaoshi.controller.util.AllRecords;
import com.zr.kucunkufangbiaoshi.controller.util.ResultBuildVo;
import com.zr.kucunkufangbiaoshi.controller.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2019/7/16 0016.
 */
@RestController
@RequestMapping("wwwwwwwww/")
public class DepotController {
    @Autowired
    private DepotService depotService;

    /**
     * 分页
     * @param depotSelectVo
     * @return
     */
    @PostMapping("queryPagedepot")
    public ResultVo<AllRecords> queryPageDepot(@RequestBody DepotSelectVo depotSelectVo){
        return depotService.queryPageDepot(depotSelectVo);

    }
    /**
     * 状态下拉框
     */
    @GetMapping("startAndStop")
    public ResultVo<List<XiaLaVo>> queryXia(){

        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (int i = 0; i< StatusEnum.values().length; i++){
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setValue(i);
            xiaLaVo.setLabel(StatusEnum.getName(i));
            xiaLaVoList.add(xiaLaVo);
        }
        return ResultBuildVo.success(xiaLaVoList);
    }
    /**
     * 修改状态接口
     * @param
     * @param bindingResult
     * @return
     */
    @PostMapping("updateDepotStatus")
    public ResultVo updateDepotStatus(@RequestBody @Valid DepotUpdateStatus depotUpdateStatus, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return depotService.updateDepotStatus(depotUpdateStatus);
    }
    /**
     * 修改接口
     */
    @PostMapping("updateDepot")
    public ResultVo updateDepot(@RequestBody @Valid DepotUpdateVo depotUpdateVo, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),
                    "500");
        }
        return depotService.updateDepot(depotUpdateVo);

    }
    /**
     * 新增
     * @param
     * @param bindingResult
     * @return
     */
    @PostMapping("addDepot")
    public ResultVo addDepot(@RequestBody @Valid DepotAddVo depotAddVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return  depotService.addDepot(depotAddVo);
    }

}
